﻿Imports System.Data.Odbc
Imports System.Data.SqlClient
Public Class frmFonacot
    Dim mdsDatos As New DataSet
    Dim total As Double
    Dim Info As New clsInfonavit
    Dim Bitacora As New clsActividad
    Private Sub frmFonacot_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip2.Cursor = Cursors.Hand
        ToolStrip2.RenderMode = System.Windows.Forms.ToolStripRenderMode.System
        ToolStrip2.Stretch = True

        Me.txtFolio.Text = (BuscarIntDatoCitra("SELECT MAX(IDRegistro) as Id FROM InfonavitFonacot")) + 1
        Me.txtIDEmpleado.Text = EmpleadoID
        Me.txtNombre.Text = frmModuloEmpleadosDetalle.txtPaterno.Text & " " & frmModuloEmpleadosDetalle.txtMaterno.Text.Trim & " " & frmModuloEmpleadosDetalle.txtNombre.Text.Trim
        Me.datFecha.Value = Now()
        Me.txtActual.Text = 0
        Me.txtNuevo.Text = 0
        Me.txtMotivo.Text = ""



        CargaCuentasContables()
    End Sub
    Sub CargaCuentasContables()
        lstCuentas.Items.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = ContabilidadConnection

        Dim strSql As String = ""

        strSql = "select * from cuentas where ctamayor=2 AND tipo='D'"


        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstCuentas.Items.Add(rdBuscar("Nombre").ToString.Trim)
        Loop
    End Sub
    Sub buscaFonacot()
        Dim cuentausuario As String = BuscarStrDatoCitra("Empleados", "CuentaCW", "IdEmpleado=" & EmpleadoID)
        cuentausuario = Mid(cuentausuario, 5)
        'Dim mes As Date = Now()
        Dim cuentacompac As String = BuscarStrDatoContabilidad("Cuentas", "Codigo", "Nombre='" & Me.lstCuentas.Text.Trim & "'")
        cuentacompac = Mid(cuentacompac, 1, 4)
        Dim codigo As String = cuentacompac & cuentausuario
        Dim idcuenta As Integer
        Dim Periodo As Integer
        Try
            idcuenta = BuscarIntDatoContabilidad("cuentas", "id", "codigo='" & codigo & "'")
            Periodo = BuscarIntDatoContabilidad("MovimientosPoliza", "max(periodo)", "idcuenta=" & idcuenta & " and tipomovto=0")
        Catch ex As Exception

        End Try


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = ContabilidadConnection

        Dim strSql As String = ""

        strSql = "select importe"
        strSql = strSql & " from MovimientosPoliza"
        strSql = strSql & " where idcuenta=" & idcuenta & " and tipomovto=0 and periodo=" & Periodo

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader


        Dim Fonacot As Double

        Do While rdBuscar.Read()
            Fonacot = rdBuscar("importe")
        Loop

        Me.txtActual.Text = Fonacot

    End Sub

    Private Sub lstCuentas_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCuentas.Leave
        buscaFonacot()
    End Sub

    Private Sub lstCuentas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCuentas.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub
    Sub gather()
        Info.idRegistro = Me.txtFolio.Text
        Info.idempleado = Me.txtIDEmpleado.Text
        Info.Fecha = Me.datFecha.Value
        Info.Original = Me.txtActual.Text
        Info.nuevo = Me.txtNuevo.Text
        Info.Motivo = Me.txtMotivo.Text
        Info.Capturo = usuarioactivo
        Info.Reportado = "No"
        Info.Tipo = "FONACOT"

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub toolGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub toolSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton5_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton5.Click
        If Me.txtNuevo.Text = "0" Or Me.txtNuevo.Text = "" Then
            MsgBox("Debes indicar el Nuevo Monto a descontar", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        gather()
        Bitacora.RegistraActividad("rRalizó una corrección de infonavit")

        Info.Insertar()
        frmModuloEmpleadosDetalle.CargaFonacotOLD()
        MsgBox("Guardado Éxitoso", MsgBoxStyle.Information, "Aviso")
        Me.Close()
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Me.Close()
    End Sub
End Class